<?php 
//彩色标签云插件
class cy_tags2 extends WP_Widget {

	function cy_tags2() {
		$widget_ops = array('description' => '随机显示彩色标签，可自定义数量，大小');
		$this->WP_Widget('cy_color_tags', '&#xE000; 彩色标签云', $widget_ops);
	}

	function form($instance) {
		$title = isset($instance['title']) ? $instance['title'] : '彩色标签';
		$counts = isset($instance['counts']) ? $instance['counts'] : 32;
		$size = isset($instance['size']) ? $instance['size'] : 12;
		$page_link=isset($instance['page_link'])? $instance['page_link']: '';
?>
	<p>
		<label>标题：
			<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
		</label>
	</p>

	<p>
		<label>标签数量：
			<input id="<?php echo $this->get_field_id('counts'); ?>" name="<?php echo $this->get_field_name('counts'); ?>" type="text" value="<?php echo $counts; ?>" size="6" />
		</label>
	</p>

	<p>
		<label>字体大小：
			<input id="<?php echo $this->get_field_id('size'); ?>" name="<?php echo $this->get_field_name('size'); ?>" type="text" value="<?php echo $size; ?>" size="6" />
		</label>
	</p>

	<p>
		<label>标签页面链接：
			<input class="widefat" id="<?php echo $this->get_field_id('page_link'); ?>" name="<?php echo $this->get_field_name('page_link'); ?>" type="text" value="<?php echo $page_link; ?>" />
		</label>
	</p>
<?php
	}

	function update($new_instance, $old_instance) { return $new_instance; }

	function widget($args, $instance) {
		extract( $args );
		$title = $instance['title'];
		$counts = $instance['counts'];
		$size = $instance['size'];
		$page_link = $instance['page_link'];
		echo $before_widget.$before_title.$title.$after_title;
		echo '<div class="cy_color_tags">';
		$tags_list = get_tags('orderby=count&order=DESC&number='.$counts);
		shuffle($tags_list);
		$colors = ini_color();
		if ($tags_list) {
			foreach( $tags_list as $tag ){
				echo '<a href="'.get_tag_link($tag).'" style="font-size:'.$size.'px;background:'.$color = $colors[rand(0,8)].'">'. $tag->name .' ('. $tag->count .')</a>'; 
			}
		} else {
			echo '暂无标签！';
		}
		echo '</div>';
		if($page_link) echo '<a class="more-tag" href="' . $page_link . '" rel=external >查看更多 →</a>';
		echo $after_widget;
	}
}
register_widget('cy_tags2');
?>